package com.issoft.studentresitor.controller.student;

import com.issoft.studentresitor.domain.pojo.Fee;
import com.issoft.studentresitor.domain.pojo.Feitem;
import com.issoft.studentresitor.domain.pojo.Result;
import com.issoft.studentresitor.service.IFeeService;
import com.issoft.studentresitor.service.IFeitemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "学生缴费内容接口")
@RestController
@RequestMapping("/student/fee")
public class StuFeeController {
    @Autowired
    private IFeeService feeService;
    @Autowired
    private IFeitemService feitemService;
    @Autowired
    private RedisTemplate redisTemplate;
    @ApiOperation(value = "查询所有缴费项目")
    @GetMapping("/queryAll")
    public Result queryAll() {
        //构建redis缓存key
        String key = "feitem";
        List<Feitem> feitemList = (List<Feitem>) redisTemplate.opsForValue().get(key);
        if (feitemList != null && feitemList.size() > 0) {
            //缓存命中
            return Result.success(feitemList);
        }
        //缓存未命中
        feitemList = feitemService.list();
        redisTemplate.opsForValue().set(key, feitemList);
        return Result.success(feitemList);
    }
    @ApiOperation(value = "批量缴费")
    @GetMapping("/batchPay")
    public Result batchPay(Integer userId, Integer[] feeItemIds) {
        if (feeItemIds == null || feeItemIds.length == 0) {
            return Result.error("请选择缴费项目");
        }
        boolean result = feeService.batchPay(userId, feeItemIds);
        return Result.success( result);
    }
    @ApiOperation(value = "根据user_id查询所有缴费信息")
    @GetMapping("/queryByUserId/{userId}")
    public Result queryByUserId(@PathVariable("userId") Integer userId) {
        List<Fee> list = feeService.queryByUserId(userId);
        return Result.success(list);
    }
}
